<?php
namespace app\common\logic;

//生成二维码
class EwmLogic
{
	/**
	 * [生成二维码]
	 * @param  [type] $id [用户ID]
	 * @param  [type] $url     [链接url]
	 */
	public function index($id,$url){
        $filename = 'upload/ewm/'.$id.'.txt';//文本路径
        $path = 'ewm/'.$id.'.png';//图片路径

        if(file_exists($filename)){
            $content = json_decode(file_get_contents($filename),true);
            $img_path = './upload/'.$content['path'];//图片路径
            $url_path = $content['url'];//url链接
            if(file_exists($img_path) && ($url_path == $url)){
                return $content['path'];
            }
        }
        vendor("phpqrcode.phpqrcode");
        $QRcode = new \QRcode();
        $level = 'L';
        $size = 7;
        /* $datas网址 $fileName要生成在哪个文件夹 $level二维码分辨率  $size二维码大小 */
        $QRcode->png($url, './upload/'.$path, $level, $size);
        $rs['path'] = $path;
       	$rs['url'] = $url;
        file_put_contents($filename,json_encode($rs));
        return $rs['path'];
	}
    /**
     * [拼接pc版邀请图片]
     * @param  [type] $avatar        [头像,相对路径]
     * @param  [type] $user_nickname [description]
     * @param  [type] $ewm           [description]
     * @param  [type] $user_id       [description]
     * @return [type]                [description]
     */
    public function create($avatar,$user_nickname,$ewm,$user_id){
        $file = '/upload/share/'.$user_id;
        $file_path = '.'.$file;
        //创建保存目录
        if(!file_exists($file_path)&&!mkdir($file_path,0777,true)){
            return array('file_name'=>'','save_path'=>'','error'=>5);
        }

        //邀请图片文件路径
        $filename = $file_path.'/pc_file.txt';
        //是否已存在拼接好的
        if(file_exists($filename)){
            $content = file_get_contents($filename);
            $content = json_decode($content,true);
            
            if($content['avatar'] == $avatar 
                && $content['user_nickname'] == $user_nickname){

                if(file_exists('.'.$content['path'])){
                    $path = $content['path'];
					return $path;
                }
            } 
        }

      
        if(empty($avatar)){
            $avatar = '/upload/none.png';
        }else{
            if(strpos($avatar, "http") === 0){
                $r = $this->getImage($avatar);
                if($r['error'] == 0){
                    $avatar =  substr($r['save_path'],1);
                    User::where('id',$user_id)->update(['avatar'=>$avatar]);
                }
            }
        }

		$avatar_path = $file.'/avatar.png';//头像路径
		$this->getThumb('.'.$avatar,68,68,'.'.$avatar_path);

		$ewm_path = $file.'/ewm.png';//二维码路径
		$this->getThumb('./upload/'.$ewm,68,68,'.'.$ewm_path);
		//拼接图片
		$image = \think\Image::open('./upload/share/base.jpg');
		$path = $file."/share.jpg";//合成图路径
		$image  ->water('.'.$avatar_path,array(20,15))
					->text($user_nickname,'./upload/share/simhei.ttf',15,'#CCD746',array(85,40),8)
					->water('.'.$ewm_path,array(390,480))
					->save('.'.$path);
	  
		//保存拼接的图片
		$data['path'] = $path;
		$data['avatar'] = $avatar;
		$data['user_nickname'] = $user_nickname;
		$data = json_encode($data);
		file_put_contents($filename,$data);   
		return $path;
    }
    /**
     * [生成缩略图]
     * @param  [type] $img  [原路径]
     * @param  [type] $x    [像素]
     * @param  [type] $y    [像素]
     * @param  [type] $path [保存路径]
     * @return [type]       [description]
     */
    public function getThumb($img,$x,$y,$path){
        $res = \think\Image::open($img);
        $res->thumb($x, $y)->save($path);
        return $path;
    }
	
	/**
     * [上传头像]
     * @param  [type] $avatar        [头像,相对路径]
     * @param  [type] $user_id       [description]
     * @return [type]                [description]
     */
	public function avatar($file,$user_id,$path){
		
		// 移动到框架应用根目录/public/upload/ 目录下
		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload'. DS . $path);
		if ($info) {
			//存入相对路径/upload/日期/文件名
			$data = DS . 'upload' . DS . $path . DS . $info->getSaveName();
			$image = \think\Image::open('.'.$data);
			// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
			$image->thumb(300, 300)->save('.'.$data);
			return [
				'code'	=>1,
				'data'  =>$data,
			];
		} else {
			// 上传失败获取错误信息
			return [
				'code'	=>0,
				'data'  =>$file->getError(),
			];
		}
	}

    //下载图片
    public function getImage($url,$type=0){
        if(trim($url)==''){
            return array('file_name'=>'','save_path'=>'','error'=>1);
        }

        $save_dir='./data/ewm';
        
        $filename = $this->create_unique().".jpg";
        if(0!==strrpos($save_dir,'/')){
            $save_dir.='/';
        }
        //创建保存目录
        if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
            return array('file_name'=>'','save_path'=>'','error'=>5);
        }
        //获取远程文件所采用的方法 
        if($type){
            $img = (new Wxtools())->getHttp($url);
        }else{
            ob_start(); 
            readfile($url);
            $img=ob_get_contents(); 
            ob_end_clean(); 
        }
        //$size=strlen($img);
        //文件大小 
        $fp2=@fopen($save_dir.$filename,'a');
        fwrite($fp2,$img);
        fclose($fp2);
        unset($img,$url);
        
        return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0);
    }
    //生成文件名
    function create_unique() {
        $data = $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR']
         .time() . rand();
        return sha1($data);
    }
}
